package mvp.levelup.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Valera on 11.05.2014.
 */
public class DateTag extends SimpleTagSupport {

    private String pattern;

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public void doTag() throws IOException, JspException {
        pattern = (pattern != null && !pattern.isEmpty()) ? pattern : "dd-MM-YYYY";
        JspWriter out = getJspContext().getOut();
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        String currentDate = dateFormat.format(new Date());
        StringWriter sw = new StringWriter();
        getJspBody().invoke(sw);
        String bodyContent = sw.toString();
        out.print(bodyContent + ": " + currentDate);
    }
}
